package com.androidcare;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;

import com.androidcare.AccountList;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningServiceInfo;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.util.Log;

public class Inicio extends Activity {
	protected AccountManager accountManager;
	protected Account account;
	boolean accountfind=false;
	String accountname;

	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		Log.i("Inicio","onCreate()");
		if(!isOnline()){
			Intent errornet = new Intent(getApplicationContext(),DialogoErrorNet.class);
			startActivity(errornet);
			finish();
		}
	}
	public void onRestart(){
		super.onRestart();
		Log.i("Inicio","onRestart()");
	}
	public void onStop(){
		super.onStop();
		Log.i("Inicio","onStop");
	}
	public void onDestroy(){
		super.onDestroy();
		Log.i("Inicio","onDestroy");
	}
	
	public void onStart(){
		super.onStart();
		

		
		
		String filename="MyFile.txt";
		File file = getBaseContext().getFileStreamPath(filename);
		
        accountManager = AccountManager.get(getApplicationContext());
        Account[] accounts = accountManager.getAccountsByType("com.google");
		
		try {
			FileInputStream fIn = getBaseContext().openFileInput(filename);
			DataInputStream dis = new DataInputStream(fIn);
			BufferedReader br = new BufferedReader(new InputStreamReader(dis));
			
			for(Account a:accounts){
				char[] inputBuffer = new char[a.name.length()];
				br.read(inputBuffer);
				String readString = new String(inputBuffer);
				if(readString.compareTo(a.name)==0){
					Intent intent = new Intent(getApplicationContext(),AppInfo.class);
					intent.putExtra("account", a);
					startActivity(intent);
				}
				else{
					Log.i("Inicio","Cuenta NO encontrada");
					
					finish();
				}
			}
		
			fIn.close();
			dis.close();
			br.close();

		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			try {
				file.createNewFile();
				Intent intent = new Intent(getApplicationContext(),AccountList.class);
				startActivity(intent);
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		finish();

	}
	
	@SuppressWarnings("unused")
	private boolean isMyServiceRunning(String name) {
	    ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
	    for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
	        if (name.equals(service.service.getClassName())) {
	            return true;
	        }
	    }
	    return false;
	}
	public boolean isOnline() {
	    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
	    NetworkInfo netInfo = cm.getActiveNetworkInfo();
	    if (netInfo != null && netInfo.isConnected()) {
	        return true;
	    }
	    return false;
	}

}
	
